package org.record.mood.utils;

import java.text.DateFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @description:校验类
 * @author: sunyw
 * @time: 2019/10/28 18:40
 */
public class StringUtils {
    public static boolean isBlank(String str) {
        int strLen = 0;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }


    public static boolean isBlank(Object obj) {
        return (obj == null || obj.equals("null") || obj.equals(""));
    }


    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }


    public static boolean isNotBlank(Object str) {
        return !isBlank(str);
    }


    public static void isNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }


    public static void notNull(Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notNull(Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }


    public static boolean isEmpty(String value) {
        return (value == null || value.trim().length() == 0 || "null".endsWith(value));
    }


    public static boolean isEmpty(Object value) {
        return (value == null || value.equals("null") || value.equals(""));
    }


    public static boolean isNotEmpty(Object value) {
        if (value != null && !"".equals(value)) {
            return true;
        }
        return false;
    }


    public static boolean isEmptyMap(Map<?, ?> value) {
        if (value != null && value.size() > 0) {
            return false;
        }
        return true;
    }


    public static boolean isNotEmptyMap(Map<?, ?> value) {
        if (value != null && value.size() > 0) {
            return true;
        }
        return false;
    }


    public static void isEmptyMap(Map<?, ?> value, String message) {
        if (value == null || value.size() == 0) {
            throw new IllegalArgumentException(message);
        }
    }


    public static boolean isEmptyList(List<?> value) {
        if (value != null && value.size() > 0) {
            return false;
        }
        return true;
    }


    public static boolean isNotEmptyList(List<?> value) {
        if (value != null && value.size() > 0) {
            return true;
        }
        return false;
    }


    public static void isEmptyList(List<?> value, String message) {
        if (value == null || value.size() == 0) {
            throw new IllegalArgumentException(message);
        }
    }


    public static boolean isNotEmpty(String str) {
        if (str != null && !"".equals(str.trim())) {
            return true;
        }
        return false;
    }


    public static boolean isNull(Object object) {
        if (object instanceof String) {
            return isEmpty(object.toString());
        }
        return (object == null);
    }


    public static boolean isNullLength(Object object) {
        if (object instanceof String) {
            return isEmptyLength(object.toString());
        }
        return (object == null);
    }


    public static boolean validIntateType(String value) {
        if (isBlank(value)){
            return false;
        }
        int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        value = value.trim();
        Locale locale = Locale.getDefault();
        DecimalFormatSymbols symbol = new DecimalFormatSymbols(locale);
        for (int i = 0; i < value.length(); i++) {
            char c = value.charAt(i);
            if (('0' > c || c > '9') && c != symbol.getGroupingSeparator() && (i != 0 || c != '-')){
                return false;
            }
        }
        NumberFormat formatName = NumberFormat.getNumberInstance(locale);
        try {
            Integer tmp = Integer.valueOf(formatName.parse(value).toString());
            if (tmp.intValue() > max || tmp.intValue() < min){
                return false;
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public static boolean valiDateTimedateType(String value) {
        if (isBlank(value)){
            return false;
        }
        try {
            DateFormat dateformat = null;
            Locale locale = Locale.getDefault();
            dateformat = DateFormat.getDateInstance(2, locale);
            dateformat.setLenient(false);
            dateformat.parse(value);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    public static boolean isEmptyLength(String value) {
        return (value == null || value.length() == 0 || "null".endsWith(value));
    }


    public static boolean equals(String str1, String str2) {
        return (str1 == null) ? ((str2 == null)) : str1.equals(str2);
    }


    public static boolean equalsIgnoreCase(String str1, String str2) {
        return (str1 == null) ? ((str2 == null)) : str1.equalsIgnoreCase(str2);
    }


}
